#!/usr/bin/env python3



import argparse

from pathlib import Path

from nunavut.jit import generate_types

import logging



# 设置日志级别

logging.basicConfig(level=logging.INFO)



def main():

    parser = argparse.ArgumentParser(description="Generate DroneCAN C headers from DSDL definitions.")

    parser.add_argument("--dsdl", type=str, help="Root directory containing DSDL files.", required=True)

    parser.add_argument("--outdir", type=str, help="Output directory for generated headers.", required=True)

    parser.add_argument("--include-experimental", action="store_true", help="Include experimental types")

    args = parser.parse_args()



    dsdl_root = Path(args.dsdl).resolve()

    output_dir = Path(args.outdir).resolve()



    print(f"Using DSDL root: {dsdl_root}")

    print(f"Outputting to: {output_dir}")



    try:

        for output_file in generate_types(

            dsdl_root,

            include_experimental=args.include_experimental,

            target_language="c"

        ):

            rel_path = output_file.relative_to(dsdl_root)

            out_file = output_dir / rel_path.with_suffix(".h")



            out_file.parent.mkdir(parents=True, exist_ok=True)

            with out_file.open("w") as f:

                f.write(output_file.read_text())

            logging.info(f"Generated: {out_file}")



        print("✅ Header generation complete.")



    except Exception as e:

        logging.error(f"❌ Failed to generate headers: {e}")

        raise



if __name__ == "__main__":

    main()
